// ------------------------------------------------------------------------------------------------
// File   : DX9_FXAA.cpp
// Date   : 2011-04-20
// Author : Weng xiao yi
// Descr. : Create an FXAA (Full Screen Anti Alias) of the frame buffer and cover the back buffer
//
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// HEADERS
// ------------------------------------------------------------------------------------------------
#include <engine_stdafx.h>
// ------------------------------------------------------------------------------------------------
// IMPLEMENTATION
// ------------------------------------------------------------------------------------------------

DX9AE_FXAA::DX9AE_FXAA()
: DX9AfterEffect()
{
}

DX9AE_FXAA::~DX9AE_FXAA()
{
}


void DX9AE_FXAA::Apply()
{	
	HRESULT hr;

	IHRRenderer* RI = GetHREngine()->GetRenderer();
	LPDIRECT3DDEVICE9 pD3DDev = (LPDIRECT3DDEVICE9)(RI->GetRealDevice());
	ICubeMaterial::MaterialList &Mat_list = ICubeMaterial::GetInstance().GetMaterialList();
	const ICubeMaterial::MaterialInfo &FXAAMaterial_info = Mat_list[ICubeMaterial::eMaterial_posteffect];

	VECTOR4FLOAT vScreenResolution;
	vScreenResolution.x = GetHR3DConfig()->GetScreenWidth();
	vScreenResolution.y = GetHR3DConfig()->GetScreenHeight();
	vScreenResolution.z = 1.0f / GetHR3DConfig()->GetScreenWidth();
	vScreenResolution.w = 1.0f / GetHR3DConfig()->GetScreenHeight();

	GetSceneEffect()->StretchBackBufferToDecolor();


	GetCubeMaterialRenderer()->PushMaterial( FXAAMaterial_info.m_material, MM_Add );
	GetCubeMaterialRenderer()->ComputeMaterialPass();
	GetCubeMaterialRenderer()->OnePassPreRenderMaterial( 0 );



	// get vertex shader for this material
	DX9VertexShaderManager::DX9VSFeatureAfterEffect oVSFeature;
	oVSFeature.stKey.ulShaderVer	 = 20;
	oVSFeature.stKey.ulEffectId      = AE_TYPE_FXAA;
	oVSFeature.stKey.ulEffectSubType = 0;
	DX9VertexShader* pVertexShader = DX9GetVertexShaderManager()->GetShader(&oVSFeature);

	// get pixel shader for this material
	DX9PixelShaderManager::DX9PSFeatureAfterEffect oPSFeature;
	oPSFeature.stKey.ulShaderVer	 = 20;
	oPSFeature.stKey.ulEffectId      = AE_TYPE_FXAA;
	oPSFeature.stKey.ulEffectSubType = 0;
	DX9PixelShader* pPixelShader = DX9GetPixelShaderManager()->GetShader(&oPSFeature);

	// use shaders
	hr = pD3DDev->SetVertexShader(pVertexShader->GetShaderInterface());
	DX9Verify( SUCCEEDED(hr) );
	hr = pD3DDev->SetPixelShader(pPixelShader->GetShaderInterface());
	DX9Verify( SUCCEEDED(hr) );

	pD3DDev->SetTexture( 0, GetSceneEffect()->GetDecolorTexture() );

	// upload constants
	if (pVertexShader->IsGlobalConstPresent( VS_GLOBAL_CONSTANT_SCREEN_RESOLUTION))
		pVertexShader->SetGlobalVector( VS_GLOBAL_CONSTANT_SCREEN_RESOLUTION, &vScreenResolution );
	if (pPixelShader->IsGlobalConstPresent( PS_GLOBAL_CONSTANT_SCREENRESOLUTION))
		pPixelShader->SetGlobalVector( PS_GLOBAL_CONSTANT_SCREENRESOLUTION, &vScreenResolution );


	RI->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

	pD3DDev->SetVertexShader(NULL);
	pD3DDev->SetPixelShader(NULL);
	pD3DDev->SetTexture( 0, NULL );
	pD3DDev->SetTexture( 1, NULL );

	GetCubeMaterialRenderer()->OnePassPostRenderMaterial( 0 );
	GetCubeMaterialRenderer()->PopMaterial();
}
